﻿#region Copyright Notice
// 
// 
// Copyright (c) 2009-2010 Anindya Chatterjee
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 
// 
// 
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Event.Plugin.Configuration;

namespace Calendar
{
    public partial class OrganizerForm : EventSettingsForm
    {
        private static CalendarEventCollection _calendarEvents;
        private CalendarEvent _calendarEvent;

        public OrganizerForm(CalendarEventCollection collection) : base(collection)
        {
            InitializeComponent();
            _calendarEvents = collection;

            LoadCalendarEvents();
        }


        private void LoadCalendarEvents()
        {
            eventsComboBox.Items.Clear();
            if (_calendarEvents.CalendarEvents != null)
            {
                eventsComboBox.Items.AddRange(_calendarEvents.CalendarEvents.ToArray());
            }
            eventsComboBox.Refresh();
        }

        private void delButton_Click(object sender, EventArgs e)
        {
            if (eventsComboBox.SelectedItem != null)
            {
                DialogResult result = MessageBox.Show("Are you want to delete the selected event?", "Confirm Delete",
                                                      MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    _calendarEvents.CalendarEvents.Remove((CalendarEvent) eventsComboBox.SelectedItem);
                    eventsComboBox.Items.Remove(eventsComboBox.SelectedItem);
                    eventsComboBox.Text = "Browse Organized Events";
                }
            }
            else
            {
                MessageBox.Show("Select an event to delete");
            }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            PopulateEventDetails();
        }

        private void PopulateEventDetails()
        {
            _calendarEvent = new CalendarEvent
                                 {
                                     EventTime = new DateTime(calendar.SelectionEnd.Year, calendar.SelectionEnd.Month,
                                                              calendar.SelectionEnd.Day, dateTimePicker.Value.Hour,
                                                              dateTimePicker.Value.Minute, dateTimePicker.Value.Second),
                                     AlarmPath = (File.Exists(alarmTextBox.Text)) ? alarmTextBox.Text : "",
                                     Description = descTextBox.Rtf,
                                     EventTitle = titleTextBox.Text,
                                     Displayed = false
                                 };
            if (_calendarEvents.CalendarEvents == null) _calendarEvents.CalendarEvents = new List<CalendarEvent>();

            _calendarEvents.CalendarEvents.Add(_calendarEvent);
            _calendarEvents.CalendarEvents.Sort(new CalendarEventComparer());

            LoadCalendarEvents();
        }

        private void eventsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var calendarEvent = eventsComboBox.SelectedItem as CalendarEvent;
            if (calendarEvent != null)
            {
                calendar.SetSelectionRange(calendarEvent.EventTime, calendarEvent.EventTime);
                dateTimePicker.Value = calendarEvent.EventTime;
                titleTextBox.Text = calendarEvent.EventTitle;
                alarmTextBox.Text = calendarEvent.AlarmPath;
                descTextBox.Rtf = calendarEvent.Description;
            }
        }

        private void deleteAllOldEventsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _calendarEvents.CalendarEvents.RemoveAll(IsOldEvent);
            LoadCalendarEvents();
        }

        private bool IsOldEvent(CalendarEvent target)
        {
            return (target.EventTime.CompareTo(DateTime.Now) < 1 && !target.Displayed);
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            var odlg = new OpenFileDialog
                           {
                               Filter = "Sound File (*.wav; *.mp3)| *.wav; *.mp3",
                               Title = "Browse your alarm sound file",
                               Multiselect = false,
                               SupportMultiDottedExtensions = true
                           };
            if (odlg.ShowDialog() == DialogResult.OK)
            {
                alarmTextBox.Text = odlg.FileName;
            }
        }
    }
}